package com.techstar.api.service;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.data.jpa.repository.JpaRepository;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techstar.api.util.GateWayApiProperties;
import com.techstar.api.util.HttpClientHeper;
import com.techstar.api.util.TrafficApiProperties;
import com.techstar.common.jackson.JacksonMapper;
import com.techstar.common.pojo.InterfaceType;
import com.techstar.common.utils.InterfaceUtils;
import com.techstar.roma.http.GatewayService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ParentOfListService<T> {

	@SuppressWarnings("rawtypes")
	protected JpaRepository beanRepos;
	
	protected GateWayApiProperties gateWayApiProperties;
	
	protected TrafficApiProperties trafficApiProperties;
	
	protected InterfaceType interfaceType;
	
	protected static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;
	
	@SuppressWarnings("unchecked")
	public void sendHttp(TypeReference<List<T>> typeReference, String[] dataName){
		if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
				||"disable".equals(trafficApiProperties.able)) {
			log.info("DistrictCongestTrendService out");
			return;
		}
		try {
			HttpGet httpGet = new HttpGet();
			httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
			log.info("httpGet is: " + httpGet.toString() +"\n");
			CloseableHttpResponse response = null;
			String result = null;
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            log.info("result: "+ result + "\n");
            JsonNode jsonNode = objectMapper.readTree(result.toString());
            try {
            	for(String name : dataName) {
            		jsonNode = jsonNode.get(name);
            	}
            }catch(Exception e) {
            	log.error(e.getMessage());
            }

            log.info("jsonNode is:" + jsonNode.toString() + "\n");
            try {
            	interfaceType.setApiUrl(httpGet.toString());
            	interfaceType.setClassName(this.getClass().getSimpleName());
            	InterfaceUtils.filtrateDate(interfaceType, DateTime.now(), jsonNode);
            } catch (Exception e) {
            	e.printStackTrace();
            	log.error("接口记录失败-"+this.getClass().getSimpleName()+":"+e.getMessage());
            }
            if(jsonNode==null)return;
			List<T> list = objectMapper.readValue(jsonNode.toString(),typeReference);
			beanRepos.saveAll(list);
            response.close();
        }catch (Exception e) {
        	log.error(e.getMessage());
        	e.printStackTrace();
        	return;
		}
	}

}
